package org.l.qiao;

import org.apache.log4j.Logger;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 严秋旺 on 2015/1/6.
 */
public class Validate {
    private static final Logger LOGGER = Logger.getLogger(Validate.class);
    /**
     * 全数字
     */
    public static final String NUMBER = "^\\d+$";
    /**
     * 整数（正数、负数、零）
     */
    public static final String INTEGER = "^\\-?\\d{1,8}$";
    /**
     * 浮点数
     */
    public static final String DOUBLE = "^\\-?\\d+\\.\\d+$";
    /**
     * 整数或小数
     */
    public static final String INTEGER_OR_DOUBLE = "^\\-?\\d+(\\.\\d+)?$";
    /**
     * 全中文
     */
    public static final String CHINESE = "^[\u4e00-\u9fa5]+$";
    /**
     * 邮箱
     */
    public static final String EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    /**
     * URL
     */
    public static final String URL = "^[a-zA-z]+://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    /**
     * 电话号码，正确格式："XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"
     */
    public static final String PHONE = "^(\\(\\d{3,4}-)|\\d{3.4}-)?\\d{7,8}$";
    /**
     * 手机号码
     */
    public static final String MOBILE = "^1[3|4|5|8][0-9]\\d{8}$";
    /**
     * 身份证号码（15位或18位数字）
     */
    public static final String ID_NUMBER = "^\\d{14}[[0-9],0-9xX]$";
    /**
     * 标准日期，例：2014-01-01
     */
    public static final String DATE = "^\\d{4}\\-\\d{2}-\\d{2}$";
    /**
     * 标准日期时间，例：2014-01-01 00:00:00
     */
    public static final String DATETIME = "^\\d{4}\\-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";
    /**
     * 常规日期，例：2014-1-1,2014年1月1日，2014.1.1
     */
    public static final String DATE_NOMAL = "^\\d{4}[年|\\-|\\.]\\d{1,2}[月|\\-|\\.]\\d{1,2}日?$";
    /**
     * 标准时间，例：01:29:45
     */
    public static final String TIME = "^\\d{2}:\\d{2}:\\d{2}$";
    /**
     * 腾讯QQ号，从10000开始
     */
    public static final String QQ = "^[1-9]\\d{4,}$";
    /**
     * 中国邮政编码
     */
    public static final String POSTALCODE = "^[1-9]\\d{5}(?!\\d)$";

    /**
     * IPv4地址
     */
    public static final String IPV4 = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";

    /**
     * IPv6地址
     */
    public static final String IPV6 = "^^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$$";

    /**
     * MD5字符串
     */
    public static final String MD5 = "^[\\dA-z]{32}$";

    /**
     * MongoDB的ObjectId
     */
    public static final String OBJECTID = "^[\\dA-z]{24}$";

    /**
     * 任意值
     */
    public static final String ANYWORD = "";

    /**
     * 验证码
     */
    public static final String REGEX_CAPTCHA = "^[\\w\\d]{4}$";

    /**
     * Map Key值的EL表达式
     * 例：
     * sss、sss.333、sss.[3].333、sss.$
     */
    public static final String EXPRESSION_KEY = "^\\w{1,20}(\\.(\\w{1,20}|(\\[\\d+\\])))*$";

    /**
     * 正则表达式验证字符串
     *
     * @param regex 正则表达式
     * @param str   待验证字符串
     * @return 验证结果，通过返回true,否则false
     */
    public static boolean check(String regex, String str) {
        try {
            if (str != null) {
                Pattern pattern = Pattern.compile(regex);
                Matcher match = pattern.matcher(str);
                return match.find();
            }
        } catch (Exception e) {
            LOGGER.error("字符验证异常：regex=" + regex + "^,string=" + str + "^\n" + e);
        }
        return false;
    }

    public static boolean check(String regex, List<String> list) {
        Pattern pattern = Pattern.compile(regex);
        for (String str : list) {
            Matcher match = pattern.matcher(str);
            if (!match.find()) {
                return false;
            }
        }
        return true;
    }

}
